import { pubRequire } from './public'
const baseURL = '/system/sts-auth'

//获取OSS临时密钥
function getOssKey(obj, callbBack, errBack) {
  return pubRequire('get', `${baseURL}`, obj, callbBack, errBack)
}
//上传OSS
function uploadOss(blob, fileName) {
  const fileEnvType = process.env.VUE_APP_ENV === 'testENV' ? 'test' : 'prod'
  getOssKey({}, (res) => {
    const client = new OSS({
      // yourRegion填写Bucket所在地域。以华东1（杭州）为例，yourRegion填写为oss-cn-hangzhou。
      region: 'oss-cn-beijing',
      // 从STS服务获取的临时访问密钥（AccessKey ID和AccessKey Secret）。
      accessKeyId: res.accessKeyId,
      accessKeySecret: res.accessKeySecret,
      // 从STS服务获取的安全令牌（SecurityToken）。
      stsToken: res.securityToken,
      // 填写Bucket名称。
      bucket: 'jn-export-record'
    })
    const uploadUrl = `export-excels-baks/${fileEnvType}/wh/${fileName}.xlsx`
    client.put(uploadUrl, blob)
  })
}
function countDownload(blob, obj) {
  let arr = []
  delete obj.pageNum
  delete obj.pageSize
  for (const key in obj) {
    if (obj[key]) arr.push(key + ':' + obj[key])
  }
  const moduleName = sessionStorage.getItem('menuName')
  const userId = JSON.parse(localStorage.getItem('userInfo')).userId
  const fileName = `moduleName:${moduleName}-userId:${userId}-${arr.join(
    '-'
  )}-date:${new Date().getTime()}`
  uploadOss(blob, fileName)
}

export { countDownload, getOssKey }
